package 俄罗斯方块接口.frame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import 俄罗斯方块接口.body.Body;
import 俄罗斯方块接口.stack.Stack;


public class Text extends BufferFrame {

	// 窗口大小
	public static final int FRAMEWIDTH = 200;

	public static final int FRAMEHEIGHT = 350;

	public int a = 0;

	public int size = 20;

	// 所有物体起始坐标
	public int start_x = 80;

	public int start_y = 50;

	public static Stack stack = new Stack();

	public static ArrayList<Body> bodys = new ArrayList<Body>();

	static Number n1;

	public Text(int width, int height) {
		super(width, height);
		this.setResizable(false);
		this.setBounds(100, 0, FRAMEWIDTH, FRAMEHEIGHT);
		this.setVisible(true);
		this.addKeyListener(new KeyMoniter());
		Body body = new Body();
		bodys.add(body);
	}

	public static void main(String[] args) {
		new Text(FRAMEWIDTH, FRAMEHEIGHT);
	}

	public void paint(Graphics g) {

		super.paint(g);
		bodys.get(bodys.size() - 1).down();

		if (bodys.get(bodys.size() - 1).isTarget == false) {
			Body body = new Body();
			bodys.add(body);
		}
		removeLine();

		// 绘图层
		for (int j = 0; j < 15; j++) {
			for (int i = 0; i < 10; i++) {
				if (stack.body_stack[j][i]) {
					g.setColor(Color.BLUE);
					g
							.fillRect(i * size, j * size + start_y, size - 1,
									size - 1);
				}
			}
		}

	}

	/*
	 * 如果一行堆满了，消除这一行，每次都要检测
	 */
	public void removeLine() {
		for (int j = 0; j < 15; j++) {// 当前行
			int x = 0;
			for (int i = 0; i < 10; i++) {
				if (stack.body_stack[j][i] == true) {// 一行的每一个
					x++;
				}
				if (x == 10) {
					for (int n = j; n >1; n--) {

						for (int k = 0; k < 10; k++) {
							stack.body_stack[n][k] = stack.body_stack[n-1][k];// 当前行等于上一行
						}
					}
				}
			}
		}
	}

	private class KeyMoniter extends KeyAdapter {

		public void keyPressed(KeyEvent e) {
			bodys.get(bodys.size() - 1).keyPressed(e);
		}
	}
}
